package cn.edu.njust.dao;

import cn.edu.njust.entity.Course;
import cn.edu.njust.entity.HitCourse;
import cn.edu.njust.entity.Login;
import cn.edu.njust.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class HitCourseDAOImpl implements HitCourseDAO{

    @Override

    public int HitC(int uId,int cId) {
        //选课逻辑：传入要选择的课，获取id，查找HitCourse表，如果该uId下已存在该课
        //返回重复选课错误标识，不存在则进行数据库操作（使用事务），添加uId,cId至数
        //据表HitCourse，同时Course表下对应cId的cNum++，退课则相反。
        PreparedStatement pstmt = null;
        PreparedStatement pstmt2 = null;
        Connection conn = null;
        int i = 0;
        int j = 0;
//        int uId = 1;
//        int cId = course.getId();
        CourseDAOImpl dao = new CourseDAOImpl();
        Course course = dao.FindById(cId);

        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);      //开启事务
//            session
            String sql = "insert into hitcourse values(?,?,null)";
            String sql2 = "update course set cNum=cNum+1 where cId=?";

            pstmt = conn.prepareStatement(sql);
            pstmt2 = conn.prepareStatement(sql2);
            pstmt.setInt(1,uId);
            pstmt.setInt(2,cId);
            pstmt2.setInt(1,cId);
            i = pstmt.executeUpdate();
            j = pstmt2.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                if(conn != null)
                    conn.rollback();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
            JDBCUtils.close(pstmt2,null);
        }
        if (i != 0 && j != 0)
            return 1;
        return 0;
    }

    @Override
    public int ExitC(int uId,int cId) {
        PreparedStatement pstmt = null;
        PreparedStatement pstmt2 = null;
        Connection conn = null;
        CourseDAOImpl dao = new CourseDAOImpl();
        Course course = dao.FindById(cId);
        int i = 0;
        int j = 0;
//        int uId = 1;
//        int cId = course.getId();
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);      //开启事务
//            session
            String sql = "delete from hitcourse where uId=? and cId=?";
            String sql2 = "update course set cNum=cNum-1 where cId=?";

            pstmt = conn.prepareStatement(sql);
            pstmt2 = conn.prepareStatement(sql2);
            pstmt.setInt(1,uId);
            pstmt.setInt(2,cId);
            pstmt2.setInt(1,cId);
            i = pstmt.executeUpdate();
            j = pstmt2.executeUpdate();
            conn.commit();
        } catch (SQLException e) {
            try {
                if(conn != null)
                    conn.rollback();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
            JDBCUtils.close(pstmt2,null);
        }
        if (i != 0 && j != 0)
            return 1;
        return 0;
    }

    @Override
    public ArrayList<HitCourse> selectAll(int uId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        CourseDAOImpl dao = new CourseDAOImpl();
        UserDAOImpl udao = new UserDAOImpl();
        ArrayList<HitCourse> list = new ArrayList<HitCourse>();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from hitcourse where uId=?";        //SQL语句
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,uId);
            rs = pstmt.executeQuery();
            while(rs.next()){
                Course course = dao.FindById(rs.getInt("cId"));
                Login user = udao.findById(uId);
                HitCourse hc= new HitCourse();
                hc.setcId(rs.getInt("cId"));
                hc.setuId(uId);
                hc.setcName(course.getName());
                hc.setuName(user.getUsername());
                list.add(hc);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return null;
    }

    @Override
    public HitCourse selectById(int uId, int cId) {         //通过id获取HitCourse对象
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        CourseDAOImpl dao = new CourseDAOImpl();
        UserDAOImpl udao = new UserDAOImpl();
        HitCourse hc = null;
//        ArrayList<HitCourse> list = new ArrayList<HitCourse>();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from hitcourse where uId=? and cId=?";        //SQL语句
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,uId);
            pstmt.setInt(2,cId);
            rs = pstmt.executeQuery();
            if (rs.next()){     //如果为真，表示查找到了结果，封装该结果返回
                Course course = dao.FindById(cId);
                Login user = udao.findById(uId);
                hc = new HitCourse();
                hc.setcId(cId);
                hc.setuId(uId);
                hc.setcName(course.getName());
                hc.setuName(user.getUsername());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return hc;
    }

    public static void main(String[] args) {
        HitCourseDAOImpl hc = new HitCourseDAOImpl();
        CourseDAOImpl cd = new CourseDAOImpl();
        Course c = cd.FindByName("离散数学");
        hc.ExitC(1,c.getId());
    }
}


